package com.mh.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Iterator;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.mh.entity.Book;
import com.mh.entity.BookType;
import com.mh.jdbc.util.StringUtil;
import com.mh.service.BookService;

public class BookAddFrame extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4745706872616847148L;
	private BookService bookService = new BookService();
	private JTextField bookName;
	private JTextField author;
	private JTextField price;
	private JRadioButton sex1;
	private JRadioButton sex2;
	private JComboBox bookType;
	private JTextArea bookDesc;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Create the frame.
	 */
	public BookAddFrame() {
		setTitle("图书添加");
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 450, 331);
		
		JLabel lblNewLabel = new JLabel("图书名称:");
		
		bookName = new JTextField();
		bookName.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("图书作者:");
		
		author = new JTextField();
		author.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("作者性别:");
		
		sex1 = new JRadioButton("男");
		buttonGroup.add(sex1);
		sex1.setName("sex");
		
		sex2 = new JRadioButton("女");
		buttonGroup.add(sex2);
		sex2.setName("sex");
		
		JLabel lblNewLabel_3 = new JLabel("图书单价:");
		
		price = new JTextField();
		price.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("图书描述:");
		
		bookDesc = new JTextArea();
		
		JButton bookAddBtn = new JButton("添加");
		bookAddBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookAddActionPerformed(e);
			}
		});
		bookAddBtn.setIcon(new ImageIcon(BookAddFrame.class.getResource("/com/mh/images/add.png")));
		
		JButton bookReset = new JButton("重置");
		bookReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookResetActionPerformed(e);
			}
		});
		bookReset.setIcon(new ImageIcon(BookAddFrame.class.getResource("/com/mh/images/reset.png")));
		
		JLabel lblNewLabel_5 = new JLabel("图书类型:");
		
		bookType = new JComboBox();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(38)
							.addComponent(bookAddBtn)
							.addPreferredGap(ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
							.addComponent(bookReset)
							.addGap(52))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_4)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookDesc, GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE))
								.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
										.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_5)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(bookType, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
										.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(bookName, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))
										.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_2)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(sex1)
											.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
											.addComponent(sex2)))
									.addGap(18)
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_1)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(author, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
										.addGroup(groupLayout.createSequentialGroup()
											.addComponent(lblNewLabel_3)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(price, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))))
							.addGap(20))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(author, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(sex1)
						.addComponent(sex2)
						.addComponent(lblNewLabel_3)
						.addComponent(price, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_5)
						.addComponent(bookType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblNewLabel_4))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(12)
							.addComponent(bookDesc, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(bookAddBtn)
						.addComponent(bookReset))
					.addContainerGap(30, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		this.fillBookType();
	}
	protected void bookResetActionPerformed(ActionEvent e) {
		bookName.setText("");
		author.setText("");
		price.setText("");
		sex1.setSelected(false);
		sex2.setSelected(false);
		bookDesc.setText("");
	}

	protected void bookAddActionPerformed(ActionEvent e) {
		String bookNameVal = bookName.getText();
		if(StringUtil.isEmpty(bookNameVal)){
			JOptionPane.showMessageDialog(null, "书名不能为空！");
			return;
		}
		if(!bookService.bookIfInsert(bookNameVal)){
			JOptionPane.showMessageDialog(null, "书名重复！");
			return;
		}
		String authorVal = author.getText();
		if(StringUtil.isEmpty(authorVal)){
			JOptionPane.showMessageDialog(null, "作者不能为空！");
			return;
		}
		String priceVal = price.getText();
		if(StringUtil.isEmpty(bookNameVal)){
			JOptionPane.showMessageDialog(null, "单价不能为空！");
			return;
		}
		BigDecimal finalPrice;
		try {
			finalPrice = new BigDecimal(priceVal);
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(null, "单价格式不正确！");
			return;
		}
		int sex = 0;
		if(sex1.isSelected()){
			sex = 1;
		}else if(sex2.isSelected()){
			sex = 2;
		}
		int bookTypeId = ((BookType)bookType.getSelectedItem()).getId();
		String bookDescVal = bookDesc.getText();
		Book book = new Book(bookNameVal, authorVal, sex, finalPrice, bookDescVal, bookTypeId);
		if(bookService.addBook(book) >0){
			JOptionPane.showMessageDialog(null, "图书添加成功！");
		}else{
			JOptionPane.showMessageDialog(null, "图书添加失败！");
		}
	}

	/**
	 * 初始化图书类别
	 */
	private void fillBookType(){
		Iterator<BookType> item = bookService.findBookType().iterator();
		while(item.hasNext()){
			bookType.addItem(item.next());
		}
	}
}
